//
//  HRJinRongViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/23.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//

#import "HRJinRongViewController.h"
#define CELLID @"CELL"
#import "HRJinRongModel.h"
#import "HRJinRongCell.h"
@interface HRJinRongViewController ()
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation HRJinRongViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self sendRequeu];
    self.tableView.rowHeight=88;
    UINib *nib=[UINib nibWithNibName:@"HRJinRongCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
}
//发送网络请求
-(void)sendRequeu
{
    NSURLSession *session=[NSURLSession sharedSession];
    NSURL *url=[NSURL URLWithString:@"http://api.21jingji.com/news/getList?subcat=759&page=1"];
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *catNameDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *arr=catNameDict[@"list"];
        for (NSDictionary *dict in arr) {
            HRJinRongModel *model=[HRJinRongModel modelWithDict:dict];
            [self.dataSourceArr addObject:model];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.tableView reloadData];
        }];
        
    }];
    [dataTask resume];
}
#pragma mark--tableView数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRJinRongCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    HRJinRongModel *model=self.dataSourceArr[indexPath.row];
    cell.model=model;
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
